package net.esj.basic.plugins.view.easyui.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import net.esj.basic.core.engine.util.tree.TreeConverter;
import net.esj.basic.pojo.TreeStructure;

public class EasyUITreeConverter implements TreeConverter{
	
	public <T extends TreeStructure> Collection convert(Collection<T> treeList){
		if(!beforeConvert(treeList)){
			return Collections.EMPTY_LIST;
		}
		if(treeList == null || treeList.isEmpty()){
			return Collections.EMPTY_LIST;
		}
		List<EasyUITreeEntity> list = new ArrayList<EasyUITreeEntity>();
		for(T struct : treeList){
			EasyUITreeEntity entity = new EasyUITreeEntity(struct.getNodeId().toString(), struct.getNodeTitle(), EasyUITreeEntity.STATE_CLOSE);
			entity.setIconCls(struct.getNodeClsIcon());
			try{
				if(struct.getParentNode()!=null){
					entity.setPId( struct.getParentNode().getNodeId().toString());
				}
				if(struct.hasChildNode()){
					Collection<EasyUITreeEntity> children = convert((Collection<T>) struct.getChildNodes());
					entity.setChildren(children);
					entity.setState(EasyUITreeEntity.STATE_CLOSE);
				}else{
					entity.setState(EasyUITreeEntity.STATE_OPEN);
				}
			}catch(Exception e){
				//e.printStackTrace();
			}
			setExtraValue(entity, struct);
			list.add(entity);
		}
		return list;
	}
	
	protected boolean beforeConvert(Collection treeList){
		return true;
	}
	
	protected void setExtraValue(EasyUITreeEntity entity,TreeStructure treeTarget){
		
	}
}